//
//  SNFriendShareVc.m
//  project
//
//  Created by 黄建华 on 2018/8/13.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNFriendShareVc.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface SNFriendShareVc ()

@property (nonatomic, strong) UILabel     *userIdLabel;
@property (nonatomic, strong) UILabel     *nickNameLabel;
@property (nonatomic, strong) UILabel     *signLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, assign) BOOL isSave;

@end

@implementation SNFriendShareVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _isSave = NO;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"friendbgimage"];
    imageView.frame = self.view.bounds;
    [self.view addSubview:imageView];
    
//    UIView *StateView = [[UIView alloc] init];
//    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
//    StateView.backgroundColor = BBIGSTATECOLOR;
//    [self.view addSubview:StateView];
    
    //    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    //    lable.textColor     = [UIColor blackColor];
    //    lable.font          = SNFONT(17);
    //    lable.textAlignment = NSTextAlignmentCenter;
    //    lable.text          = HHH_Text(@"邀请好友");
    //    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入白"];
    [leftButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(screenWidth - 44 -5, 33 + FitSimpleIpx(0, 20), 44, 44);
    [shareButton setImageNormal:@"分享"];
    shareButton.center = CGPointMake(screenWidth - 22 -10, leftButton.centerY);
    [self.view addSubview:shareButton];
    [shareButton addTarget:self action:@selector(shareButtonFriendButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *qrCode = [[UIImageView alloc] init];
    qrCode.frame = SNFRAME(screenWidth/2 - 90, 100, 180, 180);
    qrCode.image = [UIImage imageNamed:@"icon_app_qrc"];
    [self.view addSubview:qrCode];
    
    
    _userIdLabel = [UILabel simpleWithRect:SNFRAME(0, qrCode.bottom + 20, screenWidth, 25) withAlignment:NSTextAlignmentCenter withFontSize:25.0 withText:[NSString stringWithFormat:@"ID:%@", SN_USER.user_id] withTextColor:[UIColor whiteColor]];
    [self.view addSubview:_userIdLabel];
    
    //    保存图片并分享
    UIButton *saveImageButton = [[UIButton alloc] init];
    saveImageButton.image = [UIImage imageNamed:@"保存图片并分享"];
    saveImageButton.frame = SNFRAME(15, HHHHEIGHT- 80, screenWidth - 30, 50);
    [self.view addSubview:saveImageButton];
    [saveImageButton setTitle:SN_Text(@"保存图片") forState:UIControlStateNormal];
    [saveImageButton setTitle:SN_Text(@"保存图片") forState:UIControlStateHighlighted];
    [saveImageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveImageButton setTitleColor:[UIColor lightTextColor] forState:UIControlStateHighlighted];
    [saveImageButton addTarget:self action:@selector(saveImageButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    _headImageView                     = [[UIImageView alloc] init];
    _headImageView.image               = SN_USER.headImage;
    _headImageView.frame               = SNFRAME(20,HHHHEIGHT*0.65, 90, 90);
    _headImageView.layer.masksToBounds = YES;
    _headImageView.layer.cornerRadius  = _headImageView.width / 2.0;
    [self.view addSubview:_headImageView];
    
    _nickNameLabel = [UILabel simpleWithRect:SNFRAME(_headImageView.right +30, _headImageView.top + 27, screenWidth/2, 20) withAlignment:NSTextAlignmentLeft withFontSize:18 withText:SN_USER.nickName withTextColor:[UIColor whiteColor]];
    [self.view addSubview:_nickNameLabel];
    
    _signLabel = [UILabel simpleWithRect:SNFRAME(_nickNameLabel.left, _nickNameLabel.bottom + 20, screenWidth - _nickNameLabel.left, 20) withAlignment:NSTextAlignmentLeft withFontSize:18 withText:SN_USER.showSign withTextColor:[UIColor whiteColor]];
    [self.view addSubview:_signLabel];
}

- (void)saveImageButtonClick {
    if (!_isSave) {
        UIImage * image = [self captureImageFromView:self.view];
        ALAssetsLibrary * library = [ALAssetsLibrary new];
        NSData * data = UIImageJPEGRepresentation(image, 1.0);
        [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
            _isSave = YES;
            SHOWMBProgressHUD(SN_Text(@"保存成功"), nil, nil, NO, 2.0);
        }];
    }
    else
    {
            SHOWMBProgressHUD(SN_Text(@"已保存"), nil, nil, NO, 2.0);
    }
}

-(UIImage *)captureImageFromView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);
    [[UIColor clearColor] setFill];
    [[UIBezierPath bezierPathWithRect:self.view.bounds] fill];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:ctx];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)shareButtonFriendButtonClick
{
    UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
    
    [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:image];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                        UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                        UIActivityTypeOpenInIBooks]];
    activityViewController.excludedActivityTypes = excludedActivityTypes;
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
    };
}

- (void)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
